<template>
  <div>
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
        :disabled="method==='view'" label-width="120px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="组名" prop="group.id" :rules="[
                  {required: true, message:'组名不能为空', trigger:'blur'}
                 ]">
              <GridSelect
                title="选择模型"
                labelName = 'name'
                labelValue = 'id'
                :value = "inputForm.group.id"
                :limit="1"
                @getValue='(value) => {inputForm.group.id=value}'
                :columns="[
                {
                  prop: 'name',
                  label: '名称'
                }
                ]"
                :searchs="[
                {
                  prop: 'name',
                  label: '名称'
                }
                ]"
                dataListUrl="/smis/mesh/jfMeshGroup/list"
                entityBeanName="jfMeshGroup"
                queryEntityUrl="/smis/mesh/jfMeshGroup/queryById">
              </GridSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="name" :rules="[
                  {required: true, message:'名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="楼层" prop="floor.id" :rules="[
                  {required: true, message:'楼层不能为空', trigger:'blur'}
                 ]">
              <SelectTree ref="floor" :props="{
                          value: 'id',             // ID字段名
                          label: 'showName',         // 显示名称
                          children: 'childNodes'    // 子级字段名
                        }" url="/smis/floor/jfFloor/treeData" :value="inputForm.floor.id" :clearable="true"
                :accordion="true" @getValue="(value) => {inputForm.floor.id=value}" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="X轴" prop="x" :rules="[
                  {required: true, message:'X轴不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.x" placeholder="请填写X轴"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="Y轴" prop="y" :rules="[
                  {required: true, message:'Y轴不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.y" placeholder="请填写Y轴"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="Z轴" prop="z" :rules="[
                  {required: true, message:'Z轴不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.z" placeholder="请填写Z轴"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="尺寸" prop="size" :rules="[
                 ]">
              <el-input v-model="inputForm.size" placeholder="请填写尺寸"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角度" prop="angle" :rules="[
                 ]">
              <el-input v-model="inputForm.angle" placeholder="请填写角度"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" style="height: 400px;">
            <el-form-item label="所在位置" prop="x"
                :rules="[
                  {required: true, message:'请选择所在位置', trigger:'blur'}
                 ]">
              <select-point ref="selectPoint" @onMapClick="mapClick"></select-point>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">关闭</el-button>
        <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import GridSelect from '@/components/gridSelect'
  import SelectPoint from '@/components/jfMap/selectPoint.vue'
  export default {
    data() {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          group: {
            id: ''
          },
          name: '',
          floor: {
            id: ''
          },
          x: '',
          y: '',
          z: '1',
          size: '',
          angle: ''
        }
      }
    },
    components: {
      SelectTree,
      GridSelect,
      SelectPoint
    },
    methods: {
      init(method, id, floorId) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建模型`
        } else if (method === 'edit') {
          this.title = '修改模型'
        } else if (method === 'view') {
          this.title = '查看模型'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/mesh/jfMeshList/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({
              data
            }) => {
              this.inputForm = this.recover(this.inputForm, data.jfMeshList)
              this.loading = false
              this.$refs.selectPoint.showFloorAndMarkPoint(this.inputForm.floor.id, this.inputForm.x, this.inputForm.y, this.inputForm.z)
              // this.$refs.selectPoint.setMarkPoint(this.inputForm.floor.id, this.inputForm.x, this.inputForm.y)
            })
          } else if (floorId !== '') {
            this.inputForm.floor.id = floorId
            this.$refs.selectPoint.showFloor(floorId)
          }
          this.$refs.selectPoint.resize();
        })
      },
      // 表单提交
      doSubmit() {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            if (this.inputForm.x === '' || this.inputForm.y === '' || this.inputForm.x === 0 || this.inputForm.y === 0) {
              this.$message.error('请在地图上点击模型所在位置')
              return
            }
            this.loading = true
            this.$http({
              url: `/smis/mesh/jfMeshList/save`,
              method: 'post',
              data: this.inputForm
            }).then(({
              data
            }) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      },
      mapClick (e) {
        this.inputForm.x = e.x
        this.inputForm.y = e.y
		this.inputForm.z = e.z
      }
    }
  }
</script>
